import 'package:eplorex/models/contact/contact_model.dart';
import 'package:eplorex/pages/message/contact/ItemCell.dart';
import 'package:eplorex/pages/message/group/group_detail_page.dart';
import 'package:eplorex/pages/message/message_conversation.dart';
import 'package:flutter/material.dart';

import '../../../dao/user_contact_dao.dart';
import '../../../models/im_common_model.dart';
import '../../../providers/user_context.dart';
import '../contact/search_widget.dart';

class ContactGroupPage extends StatefulWidget {
  const ContactGroupPage({super.key});

  @override
  State<ContactGroupPage> createState() => _ContactGroupPageState();
}

class _ContactGroupPageState extends State<ContactGroupPage> {
  Future<List<ContactModel>?>? _contacts;
  List<ContactModel> _contactsData=[];
  List<ContactModel> _contactDynamicsData=[];

  bool _load=false;

  @override
  void initState() {
    _contacts = UserContactDao.queryUserContactGroup();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("群"),
      ),
      body: Column(
        children: [
          SearchWidget(
            onSearchChange: (str) {
              _contactDynamicsData.clear();
              if(str.isNotEmpty){
                for(ContactModel item in _contactsData){
                  if(item.name!.contains(str)){
                    _contactDynamicsData.add(item);
                  }
                }
              }else{
                _contactDynamicsData.addAll(_contactsData);
              }
              setState(() {});
            },
          ),
        FutureBuilder(
            future: _contacts,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting ||
                  !snapshot.hasData) {
                return Text("loading..");
              }
              _contactsData = snapshot.data;
              if(!_load){
                _load=true;
                _contactDynamicsData.addAll(_contactsData);
              }

              return ListView.builder(
                shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) =>
                      itemBuilder(_contactDynamicsData, context, index),
                  itemCount: _contactDynamicsData?.length);
            })
      ],),
    );
  }

  Widget? itemBuilder(
      List<ContactModel>? contactModels, BuildContext context, int index) {
    ContactModel contactModel = contactModels![index];
    return ItemCell(
      name: contactModel.name ?? '未知',
      imageUrl: contactModel.avatar,
      onAvatarTap: () {
        {
          Navigator.of(context).pushReplacement(MaterialPageRoute(
              builder: (_) => ConversationDetailPage(
                  contactModel.name!,
                  0,
                  convType: ConvType.group.value,
                  UserContext.cxt()!.uid,
                  contactModel.groupId)));
        }
      },
    );
  }
}
